#include "stdafx.h"
#include "FileTool.h"

FileTool::~FileTool()
{
	if (ifs.is_open()) {
		ifs.close();
	}
	if (ofs.is_open()) {
		ofs.flush();
		ofs.close();
	}
}

void FileTool::SetFileName(std::string file, bool bReadOrWrite)
{
	// f = fopen("ink.data", "wb");
	if (bReadOrWrite) {
		ifs.open(file, std::ios::in);
	}
	else {
		ofs.open(file, std::ios::out);
	}
}

void FileTool::WriteString(std::string data)
{
	/*char buff[1024];
	memset(buff, 0, 1024);
	int numChars = sprintf(buff, "%s", data);
	fwrite(buff, 1, numChars, f);*/
	ofs << data;
	ofs << std::endl;
}

void FileTool::WriteFloat(float fNum)
{
	/*char buff[1024];
	memset(buff, 0, 1024);
	int numChars = sprintf(buff, "%.2f", fNum);
	fwrite(buff, 1, numChars, f);*/
	//ofs << fNum;
	ofs.write(const_cast<const char*> ((char*) & fNum), 4);
}

void FileTool::WriteInt(int n)
{
	/*char buff[1024];
	memset(buff, 0, 1024);
	int numChars = sprintf(buff, "%d", n);
	fwrite(buff, 1, numChars, f);*/
	ofs << n<<std::endl;
	//ofs.write(const_cast<const char*> ((char*)&n), 4);
	//ofs.write((void*) & n, 4);
}

void FileTool::WritePoint(float x, float y)
{
	char buff[1024];
	memset(buff, 0, 1024);
	int numChars = sprintf_s(buff, "%.2f,%.2f ", x, y);
	//fwrite(buff, 1, numChars, f);
	ofs.write(buff,numChars);
}
void FileTool::WriteNewLine() {
	ofs << std::endl;
}
void FileTool::ReadString(std::string& src)
{
	//fread()
	ifs >> src;
	std::cout << src<<std::endl;
}

void FileTool::ReadFloat(float& fv)
{
	ifs >> fv;
}
float FileTool::ReadFloat() {
	float fv;
	ifs >> fv;
	return fv;
}

void FileTool::ReadInt(int& fv)
{
	ifs >> fv;
}
int FileTool::ReadInt() {
	int iv = 0;
	ifs >> iv;
	//ifs.read((char*) & iv, 4);
	return iv;
}

void FileTool::ReadPoint(float& fx, float& fy)
{
	std::string pointStr;
	ifs >> pointStr;
	sscanf_s(pointStr.data(), "%f,%f", &fx, &fy);

}
